package com.vst.pushlib;

import java.util.Hashtable;

import android.content.Context;
import android.graphics.Bitmap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class TdCode {

    public static Bitmap getTdCodeBitmap(final int width, final int height, String content) {
        Hashtable<EncodeHintType, Object> qrParam = new Hashtable<EncodeHintType, Object>();
        qrParam.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        qrParam.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        qrParam.put(EncodeHintType.MARGIN, 0);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter()
                    .encode(content, BarcodeFormat.QR_CODE, width, height, qrParam);
            final int w = bitMatrix.getWidth();
            final int h = bitMatrix.getHeight();
            final int[] data = new int[w * h];
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    if (bitMatrix.get(x, y))
                        data[y * w + x] = 0xff000000;// black
                    else
                        data[y * w + x] = -1;// -1 0xffffffff while
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(data, 0, w, 0, 0, w, h);
            return bitmap;
        } catch (WriterException e) {
        }
        return null;
    }

    public static Bitmap getTdLoginBitmap(final int width, final int height, final Context cxt) {
        return getTdCodeBitmap(width, height,
                "http://my.91vst.com/wap/login.html?user_id=" + PushConstant.getString(cxt, PushConstant.EXTRA_USER_ID)
                        + "&com=" + cxt.getPackageName());
    }
}
